package com.qf.web.controller;

import com.qf.pojo.ReaderInfo;
import com.qf.service.ReaderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 读者操作
 */
@Slf4j
@Controller
@RequestMapping("/readerinfo")
public class ReaderInfoController {

    @Autowired
    private ReaderInfoService readerInfoService;

    /**
     * 查询用户
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(){
        log.info("ReaderInfoController list ......");
        List<ReaderInfo> list = readerInfoService.findAll();
        ModelAndView modelAndView = new ModelAndView("readerinfo-list");
        modelAndView.addObject("readerInfo",list);
        return modelAndView;
    }

    /**
     * 删除用户
     * @param readerId
     * @return
     */
    @GetMapping("/deletereader.html")
    public ModelAndView delete(Long readerId){
        log.info("ReaderInfoController delete ......");
        readerInfoService.delete(readerId);
        ModelAndView modelAndView = new ModelAndView("redirect:/readerinfo/list");
        modelAndView.addObject("succ","删除成功");
        return modelAndView;
    }

    /**
     * 增加用户界面
     * @param readerId
     * @return
     */
    @GetMapping("/addui")
    public ModelAndView addUI(Long readerId){
        log.info("ReaderInfoController addui ......");
        ModelAndView modelAndView = new ModelAndView("readerinfo-add");
        return modelAndView;
    }

    /**
     * 增加用户
     * @param readerInfo
     * @return
     */
    @PostMapping("/add")
    public ModelAndView add(ReaderInfo readerInfo){
        log.info("ReaderInfoController add ......");
        readerInfoService.addReader(readerInfo);
        ModelAndView modelAndView = new ModelAndView("redirect:/readerinfo/list");
        return modelAndView;
    }

    /**
     * 修改用户界面
     * @param readerId
     * @return
     */
    @GetMapping("/updateReader.html")
    public ModelAndView updateUI(Long readerId){
        log.info("ReaderInfoController updateUI ......");
        ReaderInfo readerInfo =  readerInfoService.findById(readerId);
        ModelAndView modelAndView = new ModelAndView("readerinfo-update");
        modelAndView.addObject("readerInfo",readerInfo);
        return modelAndView;
    }

    /**
     * 修改用户
     * @param readerInfo
     * @return
     */
    @PostMapping("/update")
    public ModelAndView update(ReaderInfo readerInfo){
        log.info("ReaderInfoController update ......");
        readerInfoService.updateReader(readerInfo);
        ModelAndView modelAndView = new ModelAndView("redirect:/readerinfo/list");
        modelAndView.addObject("succ","更新成功");
        return modelAndView;
    }

    /**
     * 修改用户界面(借阅方
     * @param readerId
     * @return
     */
    @GetMapping("/updateReaderUI.html")
    public ModelAndView updateReaderUI(Long readerId){
        log.info("ReaderInfoController updateReaderUI ......");
        ReaderInfo readerInfo =  readerInfoService.findById(readerId);
        ModelAndView modelAndView = new ModelAndView("reader-update");
        modelAndView.addObject("readerInfo",readerInfo);
        return modelAndView;
    }

    /**
     * 修改用户信息(借阅方
     * @param readerInfo
     * @return
     */
    @PostMapping("/updateReader")
    public ModelAndView updateReader(ReaderInfo readerInfo){
        log.info("ReaderInfoController updateReader ......");
        readerInfoService.updateReader(readerInfo);
        ModelAndView modelAndView = new ModelAndView("redirect:/readerinfo/user?readerId="+ readerInfo.getReaderId()+"");
        modelAndView.addObject("succ","更新成功");
        return modelAndView;
    }

    /**
     * 通过id查询用户(借阅方
     * @return
     */
    @GetMapping("/user")
    public ModelAndView user(Long readerId){
        log.info("ReaderInfoController user ......" + readerId);
        ReaderInfo readerInfo = readerInfoService.findById(readerId);
        ModelAndView modelAndView = new ModelAndView("reader-info");
        modelAndView.addObject("readerinfo",readerInfo);
        return modelAndView;
    }

    /**
     * 跳转到改密码界面
     *  @return
     */
    @GetMapping("/updateUI.html")
    public ModelAndView updateUI() {
        log.info("ReaderInfoController updateUI ......");
        ModelAndView modelAndView = new ModelAndView("reader-repassword");
        return modelAndView;
    }

        @PostMapping("/updatepassword")
        public ModelAndView updatepassword(ReaderInfo readerInfo, String newPasswd, HttpSession session){
        log.info("ReaderInfoController updatepassword ......");
        readerInfo.setPassword(newPasswd);
        readerInfoService.updatePassword(readerInfo);
        ModelAndView modelAndView = new ModelAndView("redirect:/reader_main.html");
        modelAndView.addObject("succ","更新成功");
        return modelAndView;
    }
}
